package de.tabuma.shopping.view;

import de.tabuma.shopping.entity.*;
import de.tabuma.lib.*;
import de.tabuma.shopping.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.logging.*;
import java.util.*;
import java.text.*;
import com.google.appengine.api.users.*;

/**
 *
 * @author $Author: alefen $
 * @version $Revision: 1.2 $
 * @since $Date: 2009/07/21 19:37:33 $
 */
public class details extends tabumaview {

    private static Logger log =  Logger.getLogger(details.class.getName());
    String orderNav;
    public details(String servlet, Hashtable parameter, String orderNav) {
        super(servlet, parameter);
        this.orderNav = orderNav;
    }

    public void processRequest(ServletContext context, HttpServletRequest request, HttpServletResponse response, PrintWriter out)  {
        try {

            UserService userService = UserServiceFactory.getUserService();
            User user = userService.getCurrentUser();
            Order myOrder = (Order) request.getAttribute(constants.ORDER);

            out.println("\t<div class=\"contentContent\">");
            out.println("\t\t<div class=\"orderDetailsFrame\">");
            out.println("\t\t\t<div class=\"contentHeader\">" + constants.getValueFromProperties("orders.details.head") + "</div>");
            
            List<LicensePackage> list = (List<LicensePackage>) request.getAttribute(constants.ORDER_LICENSEPACKAGES);

            NumberFormat nf = NumberFormat.getCurrencyInstance();
            nf.setCurrency(Currency.getInstance(Locale.GERMANY));

            DateFormat df = DateFormat.getDateTimeInstance();

            out.println("\t\t\t<div class=\"contentContent\">");
            out.println("\t\t\t\t<div class=\"orderDetails1\">");
            out.println("\t\t\t\t\t<div class=\"orderDetailsleft\">"+constants.getValueFromProperties("orders.details.ordernr")+"</div>");
            out.println("\t\t\t\t\t<div class=\"orderDetailsright\">"+myOrder.getId()+"</div>");
            out.println("\t\t\t\t</div>");
            if(userService.isUserAdmin()) {
                out.println("\t\t\t\t<div class=\"orderDetails2\">");
                out.println("\t\t\t\t\t<div class=\"orderDetailsleft\">"+myOrder.getToken()+"</div>");
                out.println("\t\t\t\t\t<div class=\"orderDetailsright\">");
                out.println("</div>");
                out.println("\t\t\t\t</div>");
            }
            else {
                out.println("\t\t\t\t<div class=\"orderDetails2\">");
                out.println("\t\t\t\t\t<div class=\"orderDetailsleft\"></div>");
                out.println("\t\t\t\t\t<div class=\"orderDetailsright\">");
                out.println("</div>");
                out.println("\t\t\t\t</div>");
            }
            out.println("\t\t\t\t<div class=\"orderDetails1\">");
            out.println("\t\t\t\t\t<div class=\"orderDetailsleft\">"+constants.getValueFromProperties("orders.details.orderdate")+"</div>");
            out.println("\t\t\t\t\t<div class=\"orderDetailsright\">"+df.format(myOrder.getOrderDate())+"</div>");
            out.println("\t\t\t\t</div>");
            out.println("\t\t\t\t<div class=\"orderDetails2\">");
            out.println("\t\t\t\t\t<div class=\"orderDetailsleft\">"+constants.getValueFromProperties("orders.details.state")+"</div>");
            switch(myOrder.getState()) {
                case constants.STATE_OPEN:
                    out.println("\t\t\t\t\t<div class=\"orderDetailsright\">"+constants.getValueFromProperties("orders.overview.state.open")+"</div>");
                    break;
                case constants.STATE_CANCELED:
                    out.println("\t\t\t\t\t<div class=\"orderDetailsright\">"+constants.getValueFromProperties("orders.overview.state.canceled")+"</div>");
                    break;
                case constants.STATE_PAYED:
                    out.println("\t\t\t\t\t<div class=\"orderDetailsright\">"+constants.getValueFromProperties("orders.overview.state.payed")+"</div>");
                    break;
                case constants.STATE_PAYED_CANCELED:
                    out.println("\t\t\t\t\t<div class=\"orderDetailsright\">"+constants.getValueFromProperties("orders.overview.state.canceled_payed")+"</div>");
                    break;
                default:
                    out.println("\t\t\t\t\t<div class=\"orderDetailsright\"></div>");
            }
            
            out.println("\t\t\t\t</div>");
            out.println("\t\t\t\t<div class=\"orderDetails1\">");
            out.println("\t\t\t\t\t<div class=\"orderDetailsleft\">"+constants.getValueFromProperties("orders.details.canceldate")+"</div>");
            if(myOrder.getCancelDate()!=null) {
                out.println("\t\t\t\t\t<div class=\"orderDetailsright\">"+df.format(myOrder.getCancelDate())+"</div>");
            }
            else {
                out.println("\t\t\t\t\t<div class=\"orderDetailsright\">-</div>");
            }
            out.println("\t\t\t\t</div>");
            out.println("\t\t\t\t<div class=\"orderDetails2\">");
            out.println("\t\t\t\t\t<div class=\"orderDetailsleft\">"+constants.getValueFromProperties("orders.details.payeddate")+"</div>");
            if(myOrder.getPayDate()!=null) {
                out.println("\t\t\t\t\t<div class=\"orderDetailsright\">"+df.format(myOrder.getPayDate())+"</div>");
            }
            else {
                out.println("\t\t\t\t\t<div class=\"orderDetailsright\">-</div>");
            }
            out.println("\t\t\t\t</div>");

            
            out.println("\t\t\t\t<div class=\"orderDetails1\">");
            out.println("\t\t\t\t\t<div class=\"orderDetailsleft\"></div>");
            out.println("\t\t\t\t\t<div class=\"orderDetailsright\"></div>");
            out.println("\t\t\t\t</div>");
            out.println("\t\t\t\t<div class=\"orderDetails2\">");
            if((myOrder.getState()==constants.STATE_OPEN && !userService.isUserAdmin()) || (userService.isUserAdmin() && myOrder.getState()==constants.STATE_OPEN)) {
                out.println(constants.getValueFromProperties("orders.details.button.paybutton"));
                out.println(this.getFormBeginn(null));
                out.println(this.getHiddenAsAttribute());
                out.println("<input type=\"hidden\" name=\""+orderNav+"\" value=\""+constants.ORDERNAV_DETAILS+"\"/>");
                out.println("<input type=\"hidden\" name=\""+constants.ORDER_NR+"\" value=\""+myOrder.getId().toString()+"\"/>");
                out.println("<input type=\"image\" name=\""+constants.ORDER_STARTPAYPAL+"\" src=\"https://www.paypal.com/de_DE/DE/i/logo/lockbox_150x65.gif\" alt=\"PayPal Logo\" value=\"true\"/>");
                out.println(this.getFormEnd());
            }
            out.println("\t\t\t\t</div>");


            out.println("\t\t\t</div>");
            out.println("\t\t</div>");
            out.println("\t</div>");

            out.println("<div class=\"contentContent\">");
            out.println("<table class=\"priceList\">");
            out.println("<tr>");
            out.println("<td colspan=\"2\" class=\"priceListHeader\">"+constants.getValueFromProperties("orders.details.licenselist.header")+"</td>");
            out.println("</tr>");

            double price = 0;
            List<OrderLicensePackageRelation> olprs = (List<OrderLicensePackageRelation>) request.getAttribute(constants.ORDER_LICENSEPACKAGERELATION_LIST);
            for(int i=0;i<olprs.size();i++) {
                for(int j=0;j<list.size();j++) {
                    if(olprs.get(i).getLicensePackageId()==list.get(j).getId()) {
                        out.println("<tr>");
                        out.println("<td class=\"orderListName\">"+list.get(j).getName()+"</td>");
                        out.println("<td class=\"orderListPrice\">"+nf.format(list.get(j).getPrice())+"</td>");
                        out.println("</tr>");
                        out.println("<tr>");
                        out.println("<td colspan=\"2\" class=\"orderListDescription\">"+list.get(j).getDescription()+"</td>");
                        out.println("</tr>");
                        price += list.get(j).getPrice();
                    }
                }
            }
            
            out.println("<tr>");
            out.println("<td class=\"orderDetailsTotalleft\">"+constants.getValueFromProperties("orders.details.licenselist.total")+"</td>");
            out.println("<td class=\"orderDetailsTotalright\">"+nf.format(price)+"</td>");
            out.println("</tr>");
            out.println("</table>");
            out.println("</div>");
            out.println("<div class=\"contentContent\">");

            out.println("<br/>");
            out.println(this.getFormBeginn(null));
            out.println(this.getHiddenAsAttribute());
            out.println("<input type=\"submit\" value=\""+constants.getValueFromProperties("orders.details.button.orderoverview")+"\" class=\"loginbutton\"/>");
            out.println("</form>");
            out.println("</div>");

            Hashtable<Integer,String> nameList = new Hashtable<Integer, String>();
            nameList.put(de.tabuma.shopping.logik.OrderFacade.ORDER_CANCEL_DATE, constants.getValueFromProperties("orders.details.history.cancel_date"));
            nameList.put(de.tabuma.shopping.logik.OrderFacade.ORDER_DATE, constants.getValueFromProperties("orders.details.history.date"));
            nameList.put(de.tabuma.shopping.logik.OrderFacade.ORDER_LICENSEPACKAGELIST, constants.getValueFromProperties("orders.details.history.licenselist"));
            nameList.put(de.tabuma.shopping.logik.OrderFacade.ORDER_PAY_DATE, constants.getValueFromProperties("orders.details.history.paydate"));
            nameList.put(de.tabuma.shopping.logik.OrderFacade.ORDER_TOKEN, constants.getValueFromProperties("orders.details.history.token"));
            nameList.put(de.tabuma.shopping.logik.OrderFacade.ORDER_USER, constants.getValueFromProperties("orders.details.history.user"));

            ArrayList<Integer> adminList = new ArrayList<Integer>();
            adminList.add(de.tabuma.shopping.logik.OrderFacade.ORDER_TOKEN);

            //tabumaview hv = new tabumaview(historyName, strUrl, nameList, adminList);
            //hv.processRequest(context, request, response, out);
            
        } 
        catch(Exception e) {
            String warning = e.getMessage()+"/"+e.getCause()+"/"+e.getClass()+"\n";
            for(int i=0;i<e.getStackTrace().length;i++) {
                warning += e.getStackTrace()[i] + "\n";
            }
            log.warning(warning);
        }
    }
}
